package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.Project;
import com.fy56.platform.logistics.service.web.IProjectService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 项目信息controller
 * Created by li on 2018-02-09.
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/controller/project")
public class ProjectController extends BaseController {

    @Autowired
    private IProjectService iProjectService;

    /**
     * 获取项目信息
     *
     * @param keyId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public Project getProject(Long keyId, HttpServletRequest request){
        Project project = new Project();
        if(keyId != null){
            project = iProjectService.selectByPrimaryKey(keyId);
        }
        return project;
    }

    /**
     * 添加保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(Project project, HttpServletRequest request) {
        ResultModel resultModel = null;
        List<Project> searchList = null;
        try {
            if (project != null) {

                //验证项目是否存在，若项目已存在，提示：该项目已经存在,请重新输入
                searchList = iProjectService.select(project);
                if (searchList != null && searchList.size() > 0) {
                    return new ResponseEntity<ResultModel>(new ResultModel("该项目已经存在,请重新输入!"), HttpStatus.OK);
                }

                //执行插入
                project.initInsert(SessionUtil.getSessionUser(request));
                resultModel = iProjectService.insert(project);

                //保存系统业务日志
                LogUtils.saveLog(request, "成功", project.getId().toString(), null, "ProjectManage", "save", "添加", JSON.toJSONString(project));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(false), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            String key = (project == null || project.getId() == null) ? null : project.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "ProjectManage", "save", "添加", JSON.toJSONString(project));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            searchList = null;
        }
    }

    /**
     * 修改保存
     *
     * @param server
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> update(Project project, HttpServletRequest request) {
        ResultModel resultModel = null;
        Project dbModel = null;
        try{
            if (project != null) {

                //根据主键查询DB中的记录
                dbModel = iProjectService.selectByPrimaryKey(project.getId());

                //如果DB中的项目名称与页面不等时，提示：项目名称不允许修改！
                if(!dbModel.getCnName().equals(project.getCnName())){
                    return new ResponseEntity<ResultModel>(new ResultModel("项目名称不允许修改!"), HttpStatus.OK);
                }else{

                    //设置修改操作员，修改时间（默认当前时间）
                    project.initUpdate(SessionUtil.getSessionUser(request));

                    //执行更新操作
                    resultModel = iProjectService.update(project);

                    //保存系统业务日志
                    LogUtils.saveLog(request,"成功",project.getId().toString(),null,"ProjectManage","update","编辑", JSON.toJSONString(project));
                }
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("项目编辑失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",project.getId().toString(),null,"ProjectManage","update","编辑", JSON.toJSONString(project));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

    /**
     * 删除
     *
     * @param keyId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        Project project = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                project = new Project();
                project.setId(keyId);
                project.setDeleteFlag(1);
                project.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iProjectService.delete(project);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"ProjectManage","delete","删除", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("项目删除失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"ProjectManage","delete","删除", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            project = null;
        }
    }

    /**
     * 批量删除
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchDelete(String keyIds, HttpServletRequest request) {
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                LinkedList<Project> list = new LinkedList<Project>();
                Project project = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        project = new Project();
                        project.setDeleteFlag(1);
                        project.initUpdate(SessionUtil.getSessionUser(request));
                        project.setId(Long.parseLong(id));
                        list.add(project);
                    }
                }

                //执行删除操作
                ResultModel resultModel = iProjectService.batchDelete(new ArrayList<Project>(list));

                //保存系统业务日志
                LogUtils.saveLog(request,"",keyIds,null,"ProjectManage","batchDelete","批量删除",null);
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("项目批量删除失败!"), HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }

}
